﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeWork2_5
{
    public interface IShape
    {
        double getSquare();//计算面积
        bool iflegal();//判断是否合法
    }
    public abstract class Shape
    {
        double square;//面积
        public abstract double getSquare();
    }
    public class Rectangle : Shape
    {
        public double width;
        public double height;
        public double angle = 90;
        public Rectangle(double w,double h)
        {
            width = w;
            height = h;
        }
        public override double getSquare()
        {
            return width * height;
        }
        public bool iflegal()
        {
            return angle == 90;
        }
    }
    public class Square : Shape
    {
        public double side;
        public double angle;
        public Square(double s)
        {
            side = s;
        }
        public override double getSquare()
        {
            return side * side;
        }
        public bool iflegal()
        {
            return angle == 90;
        }
    }
    public class Triangle : Shape
    {
        public double side1;
        public double side2;
        public double side3;
        public double heightOfSide1;//边1的高
        public Triangle(double s1,double s2,double s3,double h)
        {
            side1 = s1;
            side2 = s2;
            side3 = s3;
            heightOfSide1 = h;
        }
        public override double getSquare()
        {
            return side1 * heightOfSide1;
        }
        public bool iflegal()
        {
            return (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1);
        }
    }
    class Factory
    {
        //静态工厂方法
        public static Shape GetShape(string arg, int[] a)//a为构造函数的参数列表
        {
            Shape product = null;
            if (arg.Equals("Rectangle"))
            {
                product = new Rectangle(a[0], a[1]);
                //初始化设置product
            }
            else if (arg.Equals("Square"))
            {
                product = new Square(a[0]);
                //初始化设置product
            } else if (arg.Equals("Triangle"))
            {
                product = new Triangle(a[0], a[1], a[2], a[3]);
            }
            return product;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Random rd = new Random();
            double areasum = 0;
            for (int i = 0; i < 10; i++)
            {
                int shapeOpt = rd.Next(1, 4);
                if (shapeOpt == 1) { //Rectangle
                    int[] a = { rd.Next(1, 8), rd.Next(1, 8) };
                    areasum += Factory.GetShape("Rectangle", a).getSquare();
                }else if (shapeOpt == 2)//Square
                {
                    int[] a = { rd.Next(1, 8) };
                    areasum += Factory.GetShape("Square", a).getSquare();
                }
                else//Triangle
                {
                    int[] a = { rd.Next(1, 8), rd.Next(1, 8), rd.Next(1, 8), rd.Next(1, 5) };
                    areasum += Factory.GetShape("Triangle", a).getSquare();
                }
            }
            Console.Write(areasum);
            Console.ReadKey();
        }
    }
}
